package wli.web.util;

public class MyUserInputCheck {
    StringBuffer message = null;

    public StringBuffer getMessage() {
	return message;
    }

    public boolean checkEmail(String email) {
	message = new StringBuffer("");
	if(email == null || email.length() <5 ){
	    message.append("E-mail format error1");
	    return false;
	}

	String format = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_@.";
	if(email.indexOf(" ") != -1) {
	    message.append("E-mail format error!");
	    return false;
	}

	String EMAIL = email.toUpperCase();
	for (int i=0; i< EMAIL.length(); i++) {
	    String temp = EMAIL.charAt(i)+"";
	    if(format.indexOf(temp) == -1) {
		message.append("E-mail format error!");
		return false; 
	    }
	} 

	if ((email.indexOf('@')==-1)  ||(email.indexOf('.')==-1)|| 
	    (email.indexOf("'") != -1)||(email.indexOf('.') >= email.length()-2)||
	    (email.indexOf('@') == email.length())) {
	    message.append("E-mail format error!");
	    return false;
	}

	return true;
    }


    public boolean checkNumber(String number) {
	message = new StringBuffer("");

	String format = "1234567890";
	if(number.indexOf(" ") != -1) {
	    return false;
	}

	for (int i=0; i< number.length(); i++) {
	    String temp = number.charAt(i)+"";
	    if(format.indexOf(temp) == -1) {
		return false; 
	    }
	} 
	return true;
    }

    public static boolean isNumber(String number) {
	String format = "1234567890";
	if(number == null) {
	    return false;
	}

	for (int i=0; i< number.length(); i++) {
	    String temp = number.charAt(i)+"";
	    if(format.indexOf(temp) == -1) {
		return false; 
	    }
	} 
	return true;
    }

    public static boolean isUrl(String url) {
	url = url.toLowerCase();
	int code = -1;
	if(url.startsWith("http://")){
	    code = httpConnectionCode.getCode(url);
	    if(code == 200) {
		return true;
	    }
	}

	if(url.startsWith("https://")){
	    code = httpsConnectionCode.getCode(url);
	    if(code == 200) {
		return true;
	    }
	}
	return false;
    }

    public static boolean checkMoney(String money) {
	/*
	String format = "1234567890.+-";
	for (int i=0; i< money.length(); i++) {
	    String temp = money.charAt(i)+"";
	    if(format.indexOf(temp) == -1) {
		return false; 
	    }
	}
	*/
	try {
	    double mny = Double.parseDouble(money);

	}
	catch(Exception e) {
	    System.out.println("MyUserInputCheck:checkMoney:money="+money);
	    return false;
	}
	return true;
    }

    public static boolean checkMoney(String money, boolean sign) {
	if(money == null || money.length()==0) {
	    return false;
	}
	String format = "1234567890.";
	if(sign) {
	    format = format+"+-";
	}

	for (int i=0; i< money.length(); i++) {
	    String temp = money.charAt(i)+"";
	    if(format.indexOf(temp) == -1) {
		System.out.println("format="+format+" input="+temp);
		return false; 
	    }
	}
	return true;
    }

    public static boolean checkDecimal(String decimal) {
	String format = "1234567890.";
	for (int i=0; i< decimal.length(); i++) {
	    String temp = decimal.charAt(i)+"";
	    if(format.indexOf(temp) == -1 && temp.length()>0) {
		return false; 
	    }
	}
	return true;
    }

    public boolean checkPhoneNumber(String phone_number) {
	message = new StringBuffer("");

	String format = "1234567890(-)";
	if(phone_number.indexOf(" ") != -1) {
	    return false;
	}

	for (int i=0; i< phone_number.length(); i++) {
	    String temp = phone_number.charAt(i)+"";
	    if(format.indexOf(temp) == -1) {
		return false; 
	    }
	} 
	if(phone_number.length()>20 || phone_number.length()<10) {
	    return false;
	}
	return true;
    }

    public boolean checkZipCode(String zip_code) {
	message = new StringBuffer("");
	String format = "1234567890-";

	if(zip_code.indexOf(" ") != -1) {
	    message.append("Please check your zip code!");
	    return false;
	}
	
	for (int i=0; i< zip_code.length(); i++) {
	    String temp = zip_code.charAt(i)+"";
	    if(format.indexOf(temp) == -1) {
		message.append("Please check your zip code!");
		return false;
	    }
	}

	if(zip_code.length()>10 || zip_code.length()<5) {
	    message.append("Please check your zip code!");
	    return false;
	}

	return true;
    }

    public boolean checkFormat(String name, String s, String format) {
	message = new StringBuffer("");
	for (int i=0; i< s.length(); i++) {
	    String temp = s.charAt(i)+"";
	    if(format.indexOf(temp) == -1) {
		if(temp.equals(" ")) {
		    temp = "SPACE";
		}
		message.append("Character ["+temp+"] is not allowed in "+name+"!");
		return false;
	    }
	}
	return true;
    }

    public static boolean checkDate(String date_str) {
	if(date_str == null) {
	    return false;
	}

	String format ="0123456789/-";
	for (int i=0; i< date_str.length(); i++) {
	    String temp = date_str.charAt(i)+"";
	    if(format.indexOf(temp) == -1) {
		return false;
	    }
	}
	String mm   = null;
	String dd   = null;
	String yyyy = null;
	int d1 =0;
	int d2 =0;
	if(date_str.indexOf("/") != -1) {
	    d1 = date_str.indexOf("/");
	    d2 = date_str.lastIndexOf("/");
	    if(d1==0 || d2 == 0 || d1 == d2) {
		return false;
	    }
	    mm   = date_str.substring(0, d1);
	    dd   = date_str.substring(d1+1, d2);
	    yyyy = date_str.substring(d2+1);
	}

	if(date_str.indexOf("-") != -1) {
	    d1 = date_str.indexOf("-");
	    d2 = date_str.lastIndexOf("-");
	    if(d1==0 || d2 == 0 || d1 == d2) {
		return false;
	    }
	    mm   = date_str.substring(0, d1);
	    dd   = date_str.substring(d1+1, d2);
	    yyyy = date_str.substring(d2+1);
	}

	if(Integer.parseInt(mm)>12 || Integer.parseInt(mm) == 0 || 
	   Integer.parseInt(dd)>31 || Integer.parseInt(dd) == 0 || 
	   Integer.parseInt(yyyy) <1900 || Integer.parseInt(yyyy) >2900) {
	    return false;
	}
	return true;
    }

    public boolean checkSize(String name, String input, int min, int max) {
	message = new StringBuffer("");
	if (input== null || input.length() < min || input.length() > max) {
	    message.append("Please choose a "+name+" between ("+min+"--"+max +") characters or numbers.");
	    return false;
	}
	return true;
    }
}
 

